/**
 * 
 * The MIT License
 * 
 * Copyright (c) 2007 the original author or authors.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.googlecode.gchartjava;

/**
 * The Data class that contains the information that will be rendered in the
 * chart.
 * 
 * This class takes an array of floats from 0 to 100. If your data does not fall
 * in that range, you need to transform it so it does. This is the case for
 * negative number, for instance.
 * 
 * For the curious, behind the scenes, this class converts the float values to the
 * Google Chart API extended encoding.
 * 
 * @author Julien Chastang
 */
public class Data {
	private static final char[] encodingChars = { 'A', 'B', 'C', 'D', 'E', 'F',
			'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
			'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
			'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
			't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
			'6', '7', '8', '9', '-', '.' };

	private static final String[] extendedEncoding = new String[4096];

	static { // Fill the extendedEncoding array in static block.
		int cnt = 0;
		for (int i = 0; i < encodingChars.length; i++) {
			for (int j = 0; j < encodingChars.length; j++) {
				extendedEncoding[cnt++] = encodingChars[i] + ""
						+ encodingChars[j];
			}
		}
	}
	
	private final int[] data;

	/**
	 * 
	 * @param data
	 *            Float data.
	 */
	public Data(final float... data) {
		//Convert the data from float to int array corresponding to the GChart API extended encoding.
		int[] d = (data == null || data.length == 0) ? new int[0] : new int[data.length];
		int i = 0;
		for (float datum : data) {
			d[i++] = (datum >= 0f && datum <= 100f) ? Math.round((datum/100f) * 4095) : -1;
		}
		this.data = d;
	}
		

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		String encodedData = "";
		for (int i = 0; i < data.length; i++) {
			encodedData += (data[i] < 0 || data[i] > 4095) ? "__" : extendedEncoding[data[i]];
		}
		return encodedData;
	}
	
	/**
	 * Get the size of the dataset.
	 * 
	 * @return Size of the dataset. 
	 */
	public int getSize(){
		return data.length;
	}
}